from scipy import empty, c_, linspace 

def eval_fem_solution_1D(grid, fem_solu):
  """
    Simple post processing routine to values a function
    represented as a vector in a 1 dimesional, lagrange P1,
    finite element space ( hat functions )
 
    grid: sequence of increasing numbers.
    fem_solu: vector of real numbers
  """

  print grid
  print fem_solu
  print grid.size, fem_solu.size 
  assert( grid.size == fem_solu.size )
  
  nIntervals = grid.size-1
  segs  = c_[grid[0: nIntervals-1], grid[1: nIntervals]]
  base  = c_[fem_solu[0: nIntervals-1], fem_solu[1: nIntervals]]
 
  xGrid = empty( (10*nIntervals, ), dtype='float')
  val   = empty( (10*nIntervals, ), dtype='float')
  
  left  = linspace(0, 1, num=10, endpoint=False)
  right = 1.0 - left 

  i = 0 
  for se, ba in zip(segs, base):
    xGrid[10*i: 10*(i+1)] = linspace(se[0], se[1], num=10, endpoint=False)
    val[10*i: 10*(i+1)]   = ba[0]*left+ba[1]*right  
    i += 1

  return xGrid, val




